package ua.com.ulitko.server.util;

import ua.com.ulitko.client.entity.Album;
import ua.com.ulitko.client.entity.Performer;
import ua.com.ulitko.client.entity.Song;
import ua.com.ulitko.server.dto.AlbumDTO;
import ua.com.ulitko.server.dto.PerformerDTO;
import ua.com.ulitko.server.dto.SongDTO;

import java.util.ArrayList;
import java.util.List;

/**
 * @author suamo ( 01.04.12 1:39 )
 */
public class TransferDTOUtil {

    public static class FromDB {
        public static ArrayList<Performer> transferPerformerList(List<PerformerDTO> dtoList) {
            ArrayList<Performer> performerList = new ArrayList<Performer>();
            for (PerformerDTO dto : dtoList) {
                performerList.add(new Performer(dto.getName(), transferAlbumList(dto.getAlbumList())));
            }
            return performerList;
        }

        private static ArrayList<Album> transferAlbumList(List<AlbumDTO> dtoList) {
            ArrayList<Album> albumList = new ArrayList<Album>();
            for (AlbumDTO dto : dtoList) {
                albumList.add(new Album(dto.getName(), transferSongList(dto.getSongList())));
            }
            return albumList;
        }

        private static ArrayList<Song> transferSongList(List<SongDTO> dtoList) {
            ArrayList<Song> songList = new ArrayList<Song>();
            for (SongDTO dto : dtoList) {
                songList.add(new Song(dto.getName(), dto.getText()));
            }
            return songList;
        }
    }

    public static class ToDB {
        public static PerformerDTO transferPerformer(Performer performer) {
            return new PerformerDTO(performer.getName(), transferAlbumList(performer.getAlbumList()));
        }

        private static ArrayList<AlbumDTO> transferAlbumList(List<Album> list) {
            ArrayList<AlbumDTO> albumList = new ArrayList<AlbumDTO>();
            for (Album obj : list) {
                albumList.add(new AlbumDTO(obj.getName(), transferSongList(obj.getSongList())));
            }
            return albumList;
        }

        private static ArrayList<SongDTO> transferSongList(List<Song> list) {
            ArrayList<SongDTO> songList = new ArrayList<SongDTO>();
            for (Song obj : list) {
                songList.add(new SongDTO(obj.getName(), obj.getText()));
            }
            return songList;
        }
    }
}
